package com.demo.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.AbstractTableModel;

import com.demo.data.Data;
import com.demo.data.Film;



public class ClientGUI extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField taHost;
	private JTextField taPort;
	private JTextField taSearch;
	private JTable jTableData;
	private JLabel labelServer;
	private JLabel labelFilm;
	private JLabel labelAuthor;
	private JButton btnConnect;
	private JButton btnSearch;
	
	private JPanel jpNorth;
	private JScrollPane jpCenter;
	private JPanel jpSouth;
	
	private Dimension screenSize;
	private BorderLayout borderLayout;
	
	private ArrayList<Film> listData;
	private Client client;
	private String columns[] = {"ID", "Name", "Country", "Category", "Year", "Length"};
	private MyTableModel model;
	
	public ClientGUI() 
	{
		init();
	}
	
	public void init() 
	{
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int desktopWidth = (int) screenSize.getWidth();
		int desktopHeight = (int) screenSize.getHeight();
		int width = 500;
		int height = 300;
		
		jpNorth = new JPanel();
		getContentPane().add(jpNorth, BorderLayout.NORTH);
		GridBagLayout gbl_panelNorth = new GridBagLayout();
		gbl_panelNorth.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_panelNorth.rowHeights = new int[] { 0, 0 };
		gbl_panelNorth.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0,0.0, Double.MIN_VALUE };
		gbl_panelNorth.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		jpNorth.setLayout(gbl_panelNorth);

		JLabel lbLeft = new JLabel("");
		GridBagConstraints gbc_left = new GridBagConstraints();
		gbc_left.insets = new Insets(0, 0, 0, 5);
		gbc_left.gridx = 0;
		gbc_left.gridy = 0;
		jpNorth.add(lbLeft, gbc_left);

		labelServer = new JLabel("Server");
		GridBagConstraints gbc_lblServer = new GridBagConstraints();
		gbc_lblServer.anchor = GridBagConstraints.WEST;
		gbc_lblServer.insets = new Insets(5, 0, 0, 5);
		gbc_lblServer.gridx = 1;
		gbc_lblServer.gridy = 0;
		jpNorth.add(labelServer, gbc_lblServer);

		
		taHost = new JTextField();
		GridBagConstraints gbc_tfHost = new GridBagConstraints();
		gbc_tfHost.insets = new Insets(5, 0, 0, 5);
		gbc_tfHost.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfHost.gridx = 2;
		gbc_tfHost.gridy = 0;
		jpNorth.add(taHost, gbc_tfHost);
		taHost.setColumns(10);
		
		JLabel labelTmp = new JLabel(":");
		GridBagConstraints gbc_lblTmp = new GridBagConstraints();
		gbc_lblTmp.insets = new Insets(5, 0, 0, 5);
		gbc_lblTmp.gridx = 3;
		gbc_lblTmp.gridy = 0;
		jpNorth.add(labelTmp, gbc_lblTmp);
		
		taPort = new JTextField();
		GridBagConstraints gbc_tfPort = new GridBagConstraints();
		gbc_tfPort.insets = new Insets(5, 0, 0, 5);
		gbc_tfPort.fill = GridBagConstraints.NONE;
		gbc_tfPort.gridx = 4;
		gbc_tfPort.gridy = 0;
		jpNorth.add(taPort, gbc_tfPort);
		taPort.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(this);
		GridBagConstraints gbc_btnConnect = new GridBagConstraints();
		gbc_btnConnect.insets = new Insets(5, 0, 0, 5);
		gbc_btnConnect.gridx = 5;
		gbc_btnConnect.gridy = 0;
		jpNorth.add(btnConnect, gbc_btnConnect);
		
		JLabel lblMarginRight = new JLabel("");
		GridBagConstraints gbc_lblMarginRight = new GridBagConstraints();
		gbc_lblMarginRight.gridx = 6;
		gbc_lblMarginRight.gridy = 0;
		jpNorth.add(lblMarginRight, gbc_lblMarginRight);
		
		lbLeft = new JLabel("");
		GridBagConstraints gbc_left1 = new GridBagConstraints();
		gbc_left1.insets = new Insets(0, 0, 0, 5);
		gbc_left1.gridx = 0;
		gbc_left1.gridy = 1;
		jpNorth.add(lbLeft, gbc_left1);

		labelFilm = new JLabel("Film");
		GridBagConstraints gbc_lblFilm = new GridBagConstraints();
		gbc_lblFilm.anchor = GridBagConstraints.WEST;
		gbc_lblFilm.insets = new Insets(5, 0, 0, 5);
		gbc_lblFilm.gridx = 1;
		gbc_lblFilm.gridy = 1;
		jpNorth.add(labelFilm, gbc_lblFilm);
		
		taSearch = new JTextField();
		GridBagConstraints gbc_tfSearch = new GridBagConstraints();
		gbc_tfSearch.insets = new Insets(5, 0, 0, 5);
		gbc_tfSearch.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfSearch.gridx = 2;
		gbc_tfSearch.gridy = 1;
		gbc_tfSearch.gridwidth = 3;
		jpNorth.add(taSearch, gbc_tfSearch);
		taSearch.setColumns(10);
		
		btnSearch = new JButton("Search   ");
		btnSearch.addActionListener(this);
		GridBagConstraints gbc_btnSearch = new GridBagConstraints();
		gbc_btnSearch.insets = new Insets(5, 0, 0, 5);
		gbc_btnSearch.gridx = 5;
		gbc_btnSearch.gridy = 1;
		jpNorth.add(btnSearch, gbc_btnSearch);	
				
		jTableData = new JTable();
		jTableData.setFillsViewportHeight(true);
		jpCenter.add(jTableData);
		jpCenter = new JScrollPane(jTableData);
		getContentPane().add(jpCenter, BorderLayout.CENTER);
		
		setTitle("Client");
		borderLayout = (BorderLayout) getContentPane().getLayout();
		borderLayout.setVgap(3);		
		setBounds((desktopWidth - width) / 2, (desktopHeight - height) / 2, 500, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
		
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() 
			{
				try 
				{
					for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
					{
						if ("Nimbus".equals(info.getName())) 
						{
							UIManager.setLookAndFeel(info.getClassName());
							break;
						}
					}
				} 
				catch (Exception e) 
				{
					try 
					{
						UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
					} 
					catch (Exception ex) 
					{
						
					}
				}
				try 
				{
					new ClientGUI();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public void updateListData() 
	{
		model = new MyTableModel(listData);
		jTableData.setModel(model);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnConnect)
		{
			String host = taHost.getText();
			int port = Integer.parseInt(taPort.getText());
			client = new Client();
			client.connect(host, port);
			new LoginGui();
		}
		else if(e.getSource() == btnSearch)
		{
			String query = taSearch.getText();
			client.sendRequest(Data.REQUEST_GET_DATA, query);
			listData = (ArrayList<Film>)client.receivedData();
			updateListData();
		}
	}
	
	class MyTableModel extends AbstractTableModel 
	{
	    private ArrayList<Film> listData;
	    public MyTableModel(ArrayList<Film> listData) 
	    {
			this.listData = listData;
		}
		@Override
		public int getColumnCount() 
		{
			return columns.length;
		}
		@Override
		public int getRowCount()
		{
			return listData.size();
		}
		@Override
		public Object getValueAt(int row, int col) 
		{
			return listData.get(row);
		}
	    
	}
}
